package com.tjp.mall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.tjp.mall.product.entity.BrandEntity;
import com.tjp.mall.product.entity.CategoryEntity;
import com.tjp.mall.product.service.BrandService;
import com.tjp.mall.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tjp.common.utils.PageUtils;
import com.tjp.common.utils.Query;

import com.tjp.mall.product.dao.CategoryBrandRelationDao;
import com.tjp.mall.product.entity.CategoryBrandRelationEntity;
import com.tjp.mall.product.service.CategoryBrandRelationService;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {


    @Autowired
    private CategoryService categoryService;

    @Autowired
    private BrandService brandService;

    @Autowired
    private CategoryBrandRelationDao relationDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
        //根据类别编号和品牌编号查询出对应的类别名称和品牌名称
        Long brandId = categoryBrandRelation.getBrandId();

        Long catelogId = categoryBrandRelation.getCatelogId();

        CategoryEntity categoryEntity = categoryService.getById(catelogId);

        BrandEntity brandEntity = brandService.getById(brandId);

        categoryBrandRelation.setBrandName(brandEntity.getName());
        categoryBrandRelation.setCatelogName(categoryEntity.getName());

        this.save(categoryBrandRelation);
    }

    @Override
    public void updateBrandNameByBrandId(Long brandId, String name) {
//        //实现方式一
//        //定义查询条件
//        QueryWrapper<CategoryBrandRelationEntity> wrapper = new QueryWrapper<>();
//        wrapper.eq("brand_id", brandId);
//
//        //查询出相关数据
//        List<CategoryBrandRelationEntity> list = this.list(wrapper);
//
//        if (!list.isEmpty()) {
//            for (CategoryBrandRelationEntity relationEntity : list) {
//                relationEntity.setBrandName(name);
//            }
//            this.updateBatchById(list);
//        }

        //实现方式二
        CategoryBrandRelationEntity entity = new CategoryBrandRelationEntity();
        entity.setBrandId(brandId);
        entity.setBrandName(name);
        this.update(entity, new UpdateWrapper<CategoryBrandRelationEntity>().eq("brand_id", brandId));

    }

    @Override
    public void updateCatNameByCatId(Long catId, String name) {
        CategoryBrandRelationEntity entity = new CategoryBrandRelationEntity();
        entity.setCatelogName(name);
        entity.setCatelogId(catId);
        this.update(entity, new UpdateWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catId));
    }


    @Override
    public List<CategoryBrandRelationEntity> categoryBrandRelation(Long catId) {

        List<CategoryBrandRelationEntity> entityList = relationDao.selectList(new QueryWrapper<CategoryBrandRelationEntity>().eq("carelog_id", catId));

        return entityList;
    }
}